package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.SjProcessConfig;
import com.ruoyi.gcgl.service.ISjProcessConfigService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 流程人员配置Controller
 * 
 * @author ruoyi
 * @date 2025-09-21
 */
@Controller
@RequestMapping("/sj/config")
public class SjProcessConfigController extends BaseController
{
    private String prefix = "sj/config";

    @Autowired
    private ISjProcessConfigService sjProcessConfigService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysDeptService deptService;

    @RequiresPermissions("gcgl:config:view")
    @GetMapping()
    public String config()
    {
        return prefix + "/config";
    }

    /**
     * 查询流程人员配置列表
     */
    @RequiresPermissions("gcgl:config:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SjProcessConfig sjProcessConfig)
    {
        startPage();
        sjProcessConfig.setDeleteFlag(1);
        List<SjProcessConfig> list = sjProcessConfigService.selectSjProcessConfigList(sjProcessConfig);
        return getDataTable(list);
    }

    /**
     * 导出流程人员配置列表
     */
    @RequiresPermissions("gcgl:config:export")
    @Log(title = "流程人员配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SjProcessConfig sjProcessConfig)
    {
    	sjProcessConfig.setDeleteFlag(1);
        List<SjProcessConfig> list = sjProcessConfigService.selectSjProcessConfigList(sjProcessConfig);
        ExcelUtil<SjProcessConfig> util = new ExcelUtil<SjProcessConfig>(SjProcessConfig.class);
        return util.exportExcel(list, "流程人员配置数据");
    }

    /**
     * 新增流程人员配置
     */
    @GetMapping("/add")
    public String add(ModelMap mmap){
    	SysDept dept = new SysDept();
    	dept.setDelFlag("0");
    	dept.setParentId(100l);
    	List<SysDept> deptlis = deptService.selectDeptList(dept);
    	mmap.put("deptlis", deptlis);
        return prefix + "/add";
    }

    /**
     * 新增保存流程人员配置
     */
    @RequiresPermissions("gcgl:config:add")
    @Log(title = "流程人员配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SjProcessConfig sjProcessConfig)
    {
    	SysDept sysDept = deptService.selectDeptById(sjProcessConfig.getDeptId());
    	if(sysDept!=null){
    		sjProcessConfig.setDept(sysDept.getDeptName());
    	}
    	
    	sjProcessConfig.setDeleteFlag(1);
    	sjProcessConfig.setCreateAt(new Date());
    	sjProcessConfig.setCreateUser(getLoginName());
    	sjProcessConfig.setUpdateAt(new Date());
    	sjProcessConfig.setUpdateUser(getLoginName());
        return toAjax(sjProcessConfigService.insertSjProcessConfig(sjProcessConfig));
    }

    /**
     * 修改流程人员配置
     */
    @RequiresPermissions("gcgl:config:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SjProcessConfig sjProcessConfig = sjProcessConfigService.selectSjProcessConfigById(id);
        mmap.put("sjProcessConfig", sjProcessConfig);
        
        SysDept dept = new SysDept();
    	dept.setDelFlag("0");
    	dept.setParentId(100l);
    	List<SysDept> deptlis = deptService.selectDeptList(dept);
    	mmap.put("deptlis", deptlis);
        return prefix + "/edit";
    }

    /**
     * 修改保存流程人员配置
     */
    @RequiresPermissions("gcgl:config:edit")
    @Log(title = "流程人员配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SjProcessConfig sjProcessConfig)
    {
    	SysDept sysDept = deptService.selectDeptById(sjProcessConfig.getDeptId());
    	if(sysDept!=null){
    		sjProcessConfig.setDept(sysDept.getDeptName());
    	}
    	
    	sjProcessConfig.setUpdateAt(new Date());
    	sjProcessConfig.setUpdateUser(getLoginName());
        return toAjax(sjProcessConfigService.updateSjProcessConfig(sjProcessConfig));
    }

    /**
     * 删除流程人员配置
     */
    @RequiresPermissions("gcgl:config:remove")
    @Log(title = "流程人员配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sjProcessConfigService.deleteSjProcessConfigByIds(ids));
    }

    @GetMapping("/invokeUser")
    public String invokeUser(){
        return prefix + "/subUser";
    }

    @PostMapping("/invokeUserlist")
    @ResponseBody
    public TableDataInfo invokeUserlist(SysUser user)
    {
        startPage();
        user.setDelFlag("0");
        List<SysUser> list = userService.selectUserList4Approve(user);
        return getDataTable(list);
    }

}